package org.apache.james.mime4j.message;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.decoder.CodecUtil;
import org.apache.james.mime4j.field.ContentTransferEncodingField;
import org.apache.james.mime4j.field.ContentTypeField;

public abstract class Entity
  implements Disposable
{
  private Body body;
  private Header header = null;
  private Entity parent;

  public Entity()
  {
    this.body = null;
    this.parent = null;
  }

  public void dispose()
  {
    if (this.body != null)
      this.body.dispose();
  }

  public Body getBody()
  {
    return this.body;
  }

  public String getCharset()
  {
    return ContentTypeField.getCharset((ContentTypeField)getHeader().getField("Content-Type"));
  }

  public String getContentTransferEncoding()
  {
    return ContentTransferEncodingField.getEncoding((ContentTransferEncodingField)getHeader().getField("Content-Transfer-Encoding"));
  }

  public Header getHeader()
  {
    return this.header;
  }

  public String getMimeType()
  {
    ContentTypeField localContentTypeField2;
    ContentTypeField localContentTypeField1 = (ContentTypeField)getHeader().getField("Content-Type");
    if (getParent() != null)
      localContentTypeField2 = (ContentTypeField)getParent().getHeader().getField("Content-Type");
    while (true)
    {
      return ContentTypeField.getMimeType(localContentTypeField1, localContentTypeField2);
      localContentTypeField2 = null;
    }
  }

  public Entity getParent()
  {
    return this.parent;
  }

  public boolean isMimeType(String paramString)
  {
    return getMimeType().equalsIgnoreCase(paramString);
  }

  public boolean isMultipart()
  {
    int i;
    ContentTypeField localContentTypeField = (ContentTypeField)getHeader().getField("Content-Type");
    if ((localContentTypeField != null) && (localContentTypeField.getBoundary() != null) && (getMimeType().startsWith("multipart/")))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void setBody(Body paramBody)
  {
    this.body = paramBody;
    paramBody.setParent(this);
  }

  public void setHeader(Header paramHeader)
  {
    this.header = paramHeader;
  }

  public void setParent(Entity paramEntity)
  {
    this.parent = paramEntity;
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException, MimeException
  {
    OutputStream localOutputStream;
    getHeader().writeTo(paramOutputStream, paramInt);
    paramOutputStream.flush();
    Body localBody = getBody();
    if ("base64".equals(getContentTransferEncoding()))
      localOutputStream = CodecUtil.wrapBase64(paramOutputStream);
    while (true)
    {
      while (true)
      {
        localBody.writeTo(localOutputStream, paramInt);
        localOutputStream.flush();
        if (localOutputStream != paramOutputStream)
          localOutputStream.close();
        return;
        if (!("quoted-printable".equals(getContentTransferEncoding())))
          break;
        localOutputStream = CodecUtil.wrapQuotedPrintable(paramOutputStream, localBody instanceof BinaryBody);
      }
      localOutputStream = paramOutputStream;
    }
  }
}